<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/19
 * Time: 17:26
 */

namespace Home\Controller;

use \Think\Controller;
use Tools\AuthController;

class PermissionController extends AuthController
{

    function permission()
    {

        $this->user_permission_list();
        $this->display();

    }

    //获取用户权限信息(列表信息)
    function user_permission_list()
    {
        $models = D('ssg_modules');
        $id = 2;
        if (!empty($_POST)) {
            $id = $_POST['send_value'];
        }
        session("send_value", $id);
        $role_operation = D('ssg_role_modules_operate');
        $roleId = $role_operation->where("id = '" . $id . "'")->field("role_auth_id")->find();
        $auth_par_par = $models->where("level=0")->select();
        $auth_par_son = $models->where("level=1")->select();
        $this->assign("role_id", explode(",", $roleId['role_auth_id']));
        $this->assign('show_permission_par', $auth_par_par);
        $this->assign('show_permission_son', $auth_par_son);
        //        $auth_par_operation = $models->where("level=2")->select();
//        $this->assign('show_permission_operation',$auth_par_operation);
    }

    //用来保存信息
    function hold_data()
    {
        $arr = $_POST['auth_id'];
        $str = implode(",", $arr);
        $ssg_modules = D("ssg_modules");
        $role = D('ssg_role_modules_operate');
        $role->where(array('id' => $_SESSION['send_value']))->save(array("role_auth_id" => $str));

        //操作的权限id
        $arr_id = $ssg_modules->where("(level = 2 or level=3) and pid in (".$role->where(array('id'=>$_SESSION['send_value']) )->field('role_auth_id')->find()['role_auth_id'].")")->field("id")->select();
        foreach ($arr_id as $k=>$v){
            $str .= ",".$v["id"];
        }
        $result = $role->where(array('id' => $_SESSION['send_value']))->save(array("role_auth_id" => $str));
        if (!$result) {

            $this->assign("error", "权限设置失败");
            $this->redirect('permission', array(), 2,"权限授予失败");
        }
        $this->redirect('permission', array(), 1,"权限授予完成");

    }
}